package com.nandi.emergencyinspection.ui.activity.macro

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.luck.picture.lib.tools.ToastUtils
import com.nandi.emergencyinspection.R
import com.nandi.emergencyinspection.bean.MacroBean
import com.nandi.emergencyinspection.bean.MacroDictBean
import com.nandi.emergencyinspection.bean.MacroListBean
import com.nandi.emergencyinspection.bean.MacroPostBean
import com.nandi.emergencyinspection.mvp.macro.MacroSaveContract
import com.nandi.emergencyinspection.mvp.macro.MacroSavePresenter
import com.nandi.emergencyinspection.ui.adapter.MarcoSaveAdapter
import com.orhanobut.hawk.Hawk
import com.tbright.ktbaselibrary.mvp.BaseMvpActivity
import kotlinx.android.synthetic.main.activity_macro.*
import kotlinx.android.synthetic.main.header_layout.*
import java.lang.reflect.Type


class MacroActivity : BaseMvpActivity<MacroSavePresenter>(), MacroSaveContract.MacroSaveView {
    private var list: MutableList<MacroBean> = ArrayList()
    private var dictList: MutableList<MacroDictBean> = ArrayList()
    var intExtra: Int = 0
    val marcoSaveAdapter = MarcoSaveAdapter(list)
    override fun getLayoutId(): Int {
        return R.layout.activity_macro
    }

    override fun initView(savedInstanceState: Bundle?) {

        intExtra = intent.getIntExtra("disastrerID", 0)
        left_icon.setOnClickListener { finish() }
        center_txt.text = "宏观观测"
        dataShow.apply {
            layoutManager = LinearLayoutManager(this@MacroActivity)
            adapter = marcoSaveAdapter
        }
        if (Hawk.contains("macro${intExtra}")) {
            val type: Type =
                object : TypeToken<MutableList<MacroBean>>() {}.type
            val get = Hawk.get<String>("macro${intExtra}", "")
            if (get.isNotEmpty()) {
                list = Gson().fromJson(get, type)
            }

            remark.setText(Hawk.get<String>("mark${intExtra}", ""))
        }
        if (list.size > 0) {
            marcoSaveAdapter.setList(list)
        } else {
            mPresenter?.getMacroDict()
        }
        upload.setOnClickListener {
            var score = 0.0
            var macroInfo: MutableList<String> = ArrayList()
            var fileIds: MutableList<String> = ArrayList()

            var fileList: MutableList<MutableMap<String, String>> = ArrayList()
            marcoSaveAdapter.data.forEach {
                if (it.isFlog!!) {
                    score += it.score
                    macroInfo.add(it.name)
                }
                if (it.photo != null) {
                    val photoMap: MutableMap<String, String> = HashMap<String, String>()
                    photoMap["name"] = it.name
                    photoMap["fileId"] = it.imgId.toString()
                    if (it.imgId != 0) {
                        fileIds.add(it.imgId.toString())
                    }
                    fileList.add(photoMap)
                }

            }
            if (fileIds.size != fileList.size) {
                return@setOnClickListener
            }
            val macroPostBean = MacroPostBean()
            macroPostBean.remark = remark.text.toString()
            macroPostBean.disasterId = intExtra.toString()
            macroPostBean.measuredData = score.toString()
            macroPostBean.macroInfo = macroInfo.joinToString(";")
            macroPostBean.fileList = fileList

            mPresenter?.postMacroDict(macroPostBean)
        }
        save.setOnClickListener {
            Hawk.put("macro${intExtra}", Gson().toJson(marcoSaveAdapter.data))
            Hawk.put("mark${intExtra}", remark.text.toString())
            ToastUtils.s(this, "保存成功")
            finish()
        }
    }


    override fun showMacroResult(result: MacroListBean?) {
        if (result?.records != null) {
            result.records?.forEach {
                dictList.forEachIndexed { index, unitBean ->
                    if (it.type == unitBean.value?.toInt()) {
                        list.add(MacroBean(unitBean.label.toString(), it.value))
                    }
                }
            }
        }
        marcoSaveAdapter.setList(list)
    }

    override fun showMacroDictResult(result: MutableList<MacroDictBean>?) {
        if (result != null) {
            dictList = result
            mPresenter?.getMacro()
        }
    }

    override fun showPostMacroResult(result: String?) {
        if (Hawk.contains("macro${intExtra}")) {
            Hawk.delete("macro${intExtra}")
        }
        ToastUtils.s(this, "成功")
        finish()
    }
}